VFG.DrawRoadMark=function(viewer,option){
	this.viewer=viewer;
	this.option=option;
	this.id=option.id;
	this.model=option.thing;
	this.position=null;
	this.tip=new VFG.Tip(viewer);
	this.enity;
	this.positions=[];
	this.LEFT_CLICK_TIMES=0
	this.init();
	
}

VFG.DrawRoadMark.prototype.init=function() {
	let _this=this;
	this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
	this.handler.setInputAction(function (movement) {
		let cartesian3=VFG.Util.getScreenToC3 (_this.viewer, movement.position, _this.enity) 
		if(Cesium.defined(cartesian3)){
			_this.LEFT_CLICK_TIMES+=1;
			if (_this.positions.length == 0) {
	        	_this.positions.push(cartesian3.clone());
	        }
			if(_this.LEFT_CLICK_TIMES>1){
				_this.positions.pop();
			}
			_this.positions.push(cartesian3);
			if(_this.LEFT_CLICK_TIMES>=2){
				if(_this.success){
					_this.success(_this.positions);
				}
				_this.destroy();
			}
		}
    },Cesium.ScreenSpaceEventType.LEFT_CLICK)
    this.handler.setInputAction(function (movement) {
    	if (_this.LEFT_CLICK_TIMES == 0){
    		_this.tip.showAt(movement.endPosition,"单击开始绘制，右键退出");
    	}else{
    		_this.tip.showAt(movement.endPosition,"绘制中，右键退出");
    	}
    	if(_this.positions.length>0){
			let cartesian3=VFG.Util.getScreenToC3 (_this.viewer,movement.endPosition, _this.enity) 
	        if(Cesium.defined(cartesian3)){
	        	_this.positions.pop();
	        	cartesian3.y += (1 + Math.random());
	            _this.positions.push(cartesian3);
	        }
			
			if(!_this.entity){
				let options={
					id:VFG.Util.getUuid(),	
					name:_this.option.name,
					code:_this.option.code,
					type:_this.option.type,
					thing:{
						coordinates : _this.positions
					}
				}
				_this.entity=new VFG.ERoadMark(_this.viewer,options);
			}else{
				_this.entity.updateCoordinates(_this.positions,true);
			}
		}
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	this.handler.setInputAction(function (movement) {
		_this.destroy();
	}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
	
},

VFG.DrawRoadMark.prototype.getRectangle=function() {
    var positions = this.getDrawPosition();
    if (positions.length < 2) return null;
    return Cesium.Rectangle.fromCartesianArray(positions);
},

VFG.DrawRoadMark.prototype.getDrawPosition=function() {
    return this.positions;
},

VFG.DrawRoadMark.prototype.createFeature=function(){
	let _this=this;
	if(!this.entity){
		let options={
			//id:this.option.id,	
			name:this.option.name,
			code:this.option.code,
			thing:{
	             coordinates :new Cesium.CallbackProperty(function (time) {
	                 return _this.getRectangle();
	             }, false)
			}
		}
		this.entity=new VFG.ERoadMark(this.viewer,options);
	}
}

VFG.DrawRoadMark.prototype.addListener=function(type,callback){
	if('success'==type){
		this.success=callback;
	}
	if('error'==type){
		this.error=callback;
	}
	if('finish'==type){
		this.finish=callback;
	}
}

VFG.DrawRoadMark.prototype.removeListener=function(type){
	if('success'==type){
		this.success=null;
	}
	if('error'==type){
		this.error=null;
	}
	if('finish'==type){
		this.finish=null;
	}
}

VFG.DrawRoadMark.prototype.destroy = function() {
	if(this.finish){
		this.finish();
	}
    if( this.handler){
    	this.handler.destroy();
    	this.handler = null;
    };
    if(this.tip){
    	this.tip.destroy();
    }
	if(this.entity){
		this.entity.destroy();
	}
	delete this.entity;
    delete this.viewer;
	return Cesium.destroyObject(this);
};
